package cn.org.tpeach.nosql.view;

import cn.org.tpeach.nosql.controller.MainController;
import cn.org.tpeach.nosql.controller.RedisApplication;
import cn.org.tpeach.nosql.enums.RedisTreeType;
import cn.org.tpeach.nosql.framework.LarkFrame;
import cn.org.tpeach.nosql.framework.ServiceProxy;
import cn.org.tpeach.nosql.redis.bean.RedisConnectInfo;
import cn.org.tpeach.nosql.redis.bean.RedisTreeItem;
import cn.org.tpeach.nosql.redis.connection.RedisLarkPool;
import cn.org.tpeach.nosql.redis.controller.BaseController;
import cn.org.tpeach.nosql.redis.controller.ResultRes;
import cn.org.tpeach.nosql.redis.service.IRedisConfigService;
import cn.org.tpeach.nosql.service.ICommonService;
import cn.org.tpeach.nosql.tools.DataStructureUtils;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;

import java.util.Iterator;

public  class RedisTreeCell extends TreeCell<RedisTreeItem> {
    private final ICommonService commonService = ServiceProxy.getBeanProxy("commonService", ICommonService.class);

    private final static Image deleteIcon = new Image(RedisTreeCell.class.getResourceAsStream("/view/img/delete.png"));
        private final static Image serverIcon = new Image(RedisTreeCell.class.getResourceAsStream("/view/img/redis_server.png"));
        private final static Image databaseIcon = new Image(RedisTreeCell.class.getResourceAsStream("/view/img/database.png"));
        private final static Image keyIcon = new Image(RedisTreeCell.class.getResourceAsStream("/view/img/key.png"));
        private final static Image loading =  new Image(RedisTreeCell.class.getResourceAsStream("/view/img/loading.gif"));
        private final static Image folderDatabaseIcon =  new Image(RedisTreeCell.class.getResourceAsStream("/view/img/folder_database.png"));
        private final static double minHeight = 22;
        private final IRedisConfigService redisConfigService = ServiceProxy.getBeanProxy("redisConfigService", IRedisConfigService.class);
        @Override
        public void commitEdit(RedisTreeItem newValue) {
            super.commitEdit(newValue);
        }

        @Override
        public void startEdit() {
            super.startEdit();
            HBox hbox = new HBox();
            TextField textField = new TextField();
            hbox.getChildren().clear();
            hbox.setAlignment(Pos.CENTER_LEFT);
            hbox.setMinHeight(minHeight);
            textField.setMinHeight(minHeight);
            textField.setText(this.getItem().getName());
            hbox.getChildren().add(textField);
            textField.requestFocus();
            TreeCell<RedisTreeItem> cell= this;
            textField.setOnKeyPressed(event -> {
                if(event.getCode() == KeyCode.ENTER){
                    cell.getTreeItem().getValue().setName(textField.getText());
                    cell.commitEdit(cell.getItem());
                }
            });
            textField.setMinWidth(hbox.getWidth());
            this.setGraphic(hbox);
        }

        @Override
        public void cancelEdit() {
            super.cancelEdit();
            drawGeneralUi();
        }

        private void drawGeneralUi() {
            HBox hbox = new HBox();
            RedisTreeItem item = this.getItem();
            hbox.getChildren().clear();
            hbox.setAlignment(Pos.CENTER_LEFT);
            Label label = new Label();
            Tooltip tooltip = new Tooltip(this.getItem().getName());
            switch (item.getType()) {
                case LOADING:
                    ImageView imageView = new ImageView(loading);
                    imageView.setFitWidth(minHeight-5);
                    imageView.setFitHeight(minHeight-5);
                    label.setGraphic(imageView);
                    break;
                case SERVER:
                    label.setGraphic(new ImageView(serverIcon));
                    break;
                case DATABASE:
                    label.setGraphic(new ImageView(databaseIcon));
                    break;
                case KEY:
                    label.setGraphic(new ImageView(keyIcon));
                    break;
                case KEY_NAMESPACE:
                    label.setGraphic(new ImageView(folderDatabaseIcon));
                    break;
                default:
            }
            label.setText(this.getItem().getName());
            if(RedisTreeType.KEY_NAMESPACE.equals(item.getType())){
                label.setText(label.getText()+"("+this.getTreeItem().getChildren().size()+")");
            }
            label.setMinHeight(minHeight);
            label.setTooltip(tooltip);
            if(item.isDelete()){
                label.setText(label.getText()+"(remove)");
                label.setDisable(item.isDelete());
            }
            hbox.getChildren().add(label);
            if (RedisTreeType.SERVER.equals(item.getType())) {
                BorderPane borderPane = new BorderPane();
                RLabel deleteLabel = new RLabel("",new ImageView(deleteIcon));
                deleteLabel.setPadding(new Insets(0, 5, 0, 0));
                deleteLabel.setTooltip(new Tooltip("删除服务"));
                RLabel editLabel = new RLabel("",new ImageView("/view/img/menu/edit.png"));
                editLabel.setPadding(new Insets(0, 5, 0, 0));
                editLabel.setTooltip(new Tooltip("编辑服务"));
                deleteLabel.setOnMouseClicked(event -> commonService.deleteServer(this.getTreeItem()));
                editLabel.setOnMouseClicked(event -> commonService.editServer(this.getTreeItem()));

                HBox labelBox = new HBox();
                labelBox.setAlignment(Pos.CENTER_RIGHT);
                labelBox.setSpacing(5);
                labelBox.getChildren().addAll(editLabel,deleteLabel);
                borderPane.setRight(labelBox);
                borderPane.setLeft(hbox);
                this.setGraphic(borderPane);
            } else {
                this.setGraphic(hbox);
            }
        }


    @Override
        protected void updateItem(RedisTreeItem item, boolean empty) {

            super.updateItem(item, empty);
            if(empty){
                this.setGraphic(null);
            }else{
                //箭头修改
//                if(this.getTreeItem().isExpanded()){
//                    this.setDisclosureNode(new ImageView(arrowRight));
//                }else{
//                    this.setDisclosureNode(new ImageView(arrowRight));
//                }
                drawGeneralUi();

            }
        }
    }